<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/20/020
 * Time: 16:09
 */

namespace App\Controller;


use App\Model\GalleryModel;
use lib\View;

class GalleryController
{
  public function gallery(){
      $gallery=new GalleryModel('gallery','gid');
      $username=isset($_SESSION['username'])?$_SESSION['username']:'';
     $gallerys=$gallery->get(['uname'=>$username]);
    // print_r($row);exit();
      return new View('gallery',['gallerys'=>$gallerys]);
  }
  public function addImage(){
      $username=isset($_SESSION['username'])?$_SESSION['username']:'';
      return new View('addimage');
  }

  public function add_gallery(){
      $g=$_POST;
     // print_r($g);exit();
      $gsrcs=$_FILES['gsrc'];
      $gallery=new GalleryModel('gallery','gid');
      $gsrc= $gallery->checkimg($gsrcs);
      //print_r($gsrc);exit();
      $g['gsrc']=$gsrc;
      $g['create_at']=date('y-m-d',time());
     if( $gallery->save($g)){
         $gallerys=$gallery->get(['uname'=>$g['uname']]);
         //print_r($gallerys);exit();
       return new View('addimage');
     }
     // print_r($g);exit();

  }
  public function delete_gallery(){
      $gid=$this->input('gid');
      $gallery=new GalleryModel('gallery','gid');

      $gallerys=$gallery->get(['gid'=>$gid]);
      $gsrcs=str_replace('/','\\',$gallerys[0]['gsrc']);
      //echo $gsrcs;exit();
      if ($gallery->delete(['gid'=>$gid])){
          $gsrc=explode(';',$gsrcs);
          for ($i=0;$i<count($gsrc);$i++){
           unlink(PUBLIC_DIR.DIRECTORY_SEPARATOR.$gsrc[$i]);
          }
      }
      echo '<script>alert("删除相册成功");history.back();</script>';
     // print_r($s);exit();

  }

  public function input($name)
    {
        return htmlspecialchars(stripslashes($_REQUEST[$name]));
    }
}